package com.xtool.livewallpaper.base

import android.app.Dialog
import android.content.Context
import android.graphics.Outline
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import androidx.annotation.ColorInt
import com.xtool.livewallpaper.R
import com.xtool.livewallpaper.databinding.DialogAlertBinding

class AlertDialog constructor(context: Context) : Dialog(context) {
    private val binding: DialogAlertBinding by lazy { DialogAlertBinding.inflate(layoutInflater) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
    }


    override fun onStart() {
        super.onStart()
        window?.apply {
            attributes.apply {
                dimAmount = 0.6f
                width = ViewGroup.LayoutParams.MATCH_PARENT
                height = ViewGroup.LayoutParams.WRAP_CONTENT
                setBackgroundDrawable(ColorDrawable(0))
            }
        }
    }

    private fun setBuilder(builder: AlertBuilder) {
        setCancelable(builder.cancelAble)
        binding.tvTitle.text = builder.title
        binding.tvContent.movementMethod = ScrollingMovementMethod.getInstance()
        binding.tvContent.text = builder.content
        binding.tvContent.gravity = builder.gravity
        binding.btnLeft.text = builder.leftText
        binding.btnRight.text = builder.rightText
        binding.btnLeft.setTextColor(builder.leftTextColor)
        binding.btnRight.setTextColor(builder.rightTextColor)
//        binding.tvTitle.visually(builder.title.isNotEmpty())
        binding.btnLeft.visually(builder.leftText.isNotEmpty())
        binding.btnRight.visually(builder.rightText.isNotEmpty())
        binding.btnLeft.setOnClickListener { builder.leftAction(this) }
        binding.btnRight.setOnClickListener { builder.rightAction(this) }
        binding.containerView.post {
            binding.containerView.round()
        }
    }

    private fun View.round() {
        outlineProvider = object : ViewOutlineProvider() {
            override fun getOutline(view: View, outline: Outline) {
                outline.setRoundRect(0, 0, view.width, view.height, 10f.dp)
            }
        }
        clipToOutline = true
    }

    class AlertBuilder(internal val context: Context) {
        internal var title: String = getString(R.string.alert_title)
        internal var content: String = ""
        internal var cancelAble: Boolean = true
        internal var gravity: Int = Gravity.CENTER

        @ColorInt
        internal var leftTextColor: Int = getColorCompat(R.color.on_surface)

        @ColorInt
        internal var rightTextColor: Int = getColorCompat(R.color.black_50)
        internal var leftText: String = getString(R.string.ok)
        internal var rightText: String = getString(R.string.cancel)
        internal var leftAction: (AlertDialog) -> Unit = { _ -> }
        internal var rightAction: (AlertDialog) -> Unit = { dialog -> dialog.dismiss() }

        fun setTitle(title: String): AlertBuilder = apply {
            this.title = title
        }

        fun setGravity(gravity: Int): AlertBuilder = apply {
            this.gravity = gravity
        }

        fun setLeftTextColor(@ColorInt color: Int): AlertBuilder = apply {
            this.leftTextColor = color
        }

        fun setRightTextColor(@ColorInt color: Int): AlertBuilder = apply {
            this.rightTextColor = color
        }

        fun setCancelAble(cancelAble: Boolean) = apply {
            this.cancelAble = cancelAble
        }

        fun setContent(content: String): AlertBuilder = apply {
            this.content = content
        }

        fun setLeftText(text: String): AlertBuilder = apply {
            this.leftText = text
        }

        fun setRightText(text: String): AlertBuilder = apply {
            this.rightText = text
        }

        fun setLeftAction(action: (AlertDialog) -> Unit): AlertBuilder = apply {
            this.leftAction = action
        }

        fun setRightAction(action: (AlertDialog) -> Unit): AlertBuilder = apply {
            this.rightAction = action
        }

        fun build(): AlertDialog = AlertDialog(context).apply {
            setBuilder(this@AlertBuilder)
        }
    }
}